/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.acceptancechecklist.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.asset.acceptancechecklist.mapper.AssetAcListMapper;
import com.fuyao.cloud.asset.acceptancechecklist.service.AssetAcListService;
import com.fuyao.cloud.asset.cti.service.AssetCtiService;
import com.fuyao.cloud.asset.entity.AssetCti;
import com.fuyao.cloud.asset.entity.ac.AssetAc;
import com.fuyao.cloud.asset.entity.ac.AssetAcList;
import com.fuyao.cloud.asset.vo.ac.AcListVO;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 验收清单
 *
 * @author liyue
 * @date 2021-12-03 09:44:19
 */
@Service
public class AssetAcListServiceImpl extends ServiceImpl<AssetAcListMapper, AssetAcList> implements AssetAcListService {

	@Resource
	private AssetCtiService fyAssetCtiService;

	@Override
	public void saveOrUpdateAcList(AssetAc entity) {
		List<AssetAcList> acList = entity.getAcList();
		List<AssetCti> ctiList = fyAssetCtiService.list();
		if(CollUtil.isNotEmpty(acList)){
			acList.forEach(ac->{
				ac.setAcId(entity.getId());
				ac.setStatus(Integer.parseInt(entity.getStatus()));
				ac.setParentId(entity.getId());
				ac.setParentCaseid(entity.getCaseid());
				ac.setBatchnumber(entity.getBatchnumber());
				ac.setContractId(entity.getContractId());
				ac.setContractName(entity.getContractName());
				ac.setContractNumber(entity.getContractNumber());
				ac.setSupplierId(entity.getSupplierId());
				ac.setSupplierName(entity.getSupplierName());
				ac.setLocationId(entity.getLocationId());
				ac.setLocationName(entity.getLocationName());
				ac.setFullLocation(entity.getFullLocation());
				ac.setMethod(entity.getMethod());
				ac.setAmount(ac.getPrice().multiply(new BigDecimal(ac.getArrivedQuantity())));
				Optional<AssetCti> ctiOptional = ctiList.stream().filter(cti -> cti.getId().equals(ac.getCtiId())).findFirst();
				if (ctiOptional.isPresent()) {
					AssetCti cti = ctiOptional.get();
					ac.setCtiName(cti.getName());
					ac.setClassifiedCode(cti.getFullcode());
					ac.setClassified(cti.getFullname());
				}
			});
			this.saveOrUpdateBatch(acList);
		}
	}

	@Override
	public List<AcListVO> getAcList(AssetAc ac) {
		List<AssetAcList> assetAcLists = this.baseMapper.selectList(Wrappers.<AssetAcList>query().lambda().eq(AssetAcList::getAcId, ac.getId()));
		if(CollUtil.isNotEmpty(assetAcLists)){
			return assetAcLists.stream().map(acList->{
				AcListVO acListVO = new AcListVO();
				BeanUtil.copyProperties(acList,acListVO);
				acListVO.setType(ac.getType());
				acListVO.setContractId(ac.getContractId());
				acListVO.setContractName(ac.getContractName());
				acListVO.setContractNumber(ac.getContractNumber());
				acListVO.setSupplierId(ac.getSupplierId());
				acListVO.setSupplierName(ac.getSupplierName());
				acListVO.setMethod(ac.getMethod());
				acListVO.setCurrencyType(ac.getCurrencyType());
				return acListVO;
			}).collect(Collectors.toList());
		}
		return Lists.newArrayList();
	}
}
